package com.huawei.fakedouyin.dialog;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;

import androidx.annotation.NonNull;

import com.huawei.fakedouyin.R;
import com.huawei.fakedouyin.appui.LoginActivity;
import com.huawei.fakedouyin.databinding.DialogLoginoutBinding;
import com.huawei.fakedouyin.manager.PlayManager;
import com.huawei.fakedouyin.util.ActivityUtil;
import com.huawei.fakedouyin.util.LiveDataSet;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 异地登录提示弹框
 */
public class LoginOutDialog extends BaseDialog {

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    DialogLoginoutBinding binding;

    public LoginOutDialog(@NonNull Context context) {
        super(context, R.style.NormalDialog);
        initView(context);
        setCancelable(false);
        setCanceledOnTouchOutside(false);
    }

    private void initView(Context context) {
        //停止service服务
        LiveDataSet.serviceStatus.postValue(true);
        binding = DialogLoginoutBinding.inflate(LayoutInflater.from(context), null, false);
        setContentView(binding.getRoot());
        binding.loginOutTip.setText("您的账号于" + simpleDateFormat.format(new Date()) + "在另一个设备上登录，本设备账号已被强制下线");
        binding.loginOutConfirm.setOnClickListener(v -> {
            dismiss();
            //重启至登陆页面
            Intent intent = new Intent(context, LoginActivity.class);
            context.startActivity(intent);
            ActivityUtil.Companion.getInstance().finishAllActivity();
        });
        getWindow().setBackgroundDrawableResource(R.color.transparent);
    }
}
